package com.by.servlet;

import com.by.dao.UserDao;
import com.by.entity.User;
import com.by.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uname = request.getParameter("uname");
        String pword = request.getParameter("pword");
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        UserDao userDao = session.getMapper(UserDao.class);
        User user = new User();
        user.setUname(uname);
        user.setPword(pword);
        User a = userDao.userLogin(user);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(a != null){
            out.println("<script>alert('登录成功');location.href='main.html'</script>");
        }else{
            out.println("<script>alert('登录失败');location.href='login.html'</script>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
